package org.yyjkcache.tasks;

import org.yyjkcache.cache.DefaultCache;
import org.yyjkcache.common.HttpGetRequest;
import org.yyjkcache.common.JedisLinkedPool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CPUTask implements Task {
    private String prefix = "http://10.1.16.153:28081/ai-background/tpm/account/9/applications/";
    private String suffix = "/stats/detail.json?spanTime=60000&interval=6000&start=0&limit=5&orderBy=ORDERBY_S1_DESC&name=CPU&tierId=";

    @Override
    public void save() {
        List<Object> list = DefaultCache.cache().getContents("Ailist");
        Map<String, Object> map = null;
        String json = null;
        StringBuilder builder = null;
        String id = null;
        String jsons[]=new String[list.size()];
        int index=0;
        for (Object object : list) {
            try {
                builder = new StringBuilder();
                map = (Map<String, Object>) object;
                id = String.valueOf(map.get("systemId"));
                builder.append(prefix).append(id).append(suffix).append(id);
                json = HttpGetRequest.reuest().getRemoteJSON(builder.toString());
                jsons[index]=json;
            } catch (IOException e) {
                e.printStackTrace();
            }
            ++index;
        }
        JedisLinkedPool.pool().listPush("CPU",jsons);
    }
}
